package com.lushu.chapitre1.graphics.elements {
import com.sfdk.palettes.BodyPaletteData;
import com.sfdk.palettes.EyePaletteData;
import com.sfdk.palettes.HairPaletteData;
import com.sfdk.palettes.Palette;

import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Dictionary;
import flash.utils.getDefinitionByName;

public class UserAvatar {
    private var _userName:String;
    private var _spritesheetInfo:Dictionary = new Dictionary();
    private var _avatarImage:BitmapData;
    private var _hairPalette:HairPaletteData;
    private var _bodyPalette:BodyPaletteData;
    private var _eyePalette:EyePaletteData;


    public function UserAvatar() {
        var avatarInfo:Object = {};

        avatarInfo["nom"] = "Nicolas"
        avatarInfo["corps"] = "normal";
        avatarInfo["sexe"] = "1";
        avatarInfo["coupe"] = "coupeM01";
        avatarInfo["frange"] = "frange01";
        avatarInfo["yeux"] = "bleu01";
        avatarInfo["haut"] = "haut02orange";
        avatarInfo["couleur"] = "roux02";
        avatarInfo["headgear"] = "";
        avatarInfo["backheadgear"] = "backheadgear01rose";

        init(avatarInfo);
    }

    public function init(avatarInfo:Object):void {
        _userName = avatarInfo["nom"];

        var defCls:Class;
        if (avatarInfo["sexe"])
            defCls = getDefinitionByName("spritesheetM") as Class;
        else
            defCls = getDefinitionByName("spritesheetF") as Class;
        var currSS:BitmapData = new defCls(1116, 1000);

        defCls = getDefinitionByName("sheetbras") as Class;
        _spritesheetInfo["bras"] = new defCls(1116, 1000);

        if (avatarInfo["haut"] != "" && avatarInfo["haut"] != null) {
            defCls = getDefinitionByName("sheet" + avatarInfo["haut"]) as Class;
            _spritesheetInfo["haut"] = new defCls(883, 582);
        }
        if (avatarInfo["bas"] != "" && avatarInfo["bas"] != null) {
            defCls = getDefinitionByName("sheet" + avatarInfo["bas"]) as Class;
            _spritesheetInfo["bas"] = new defCls(883, 582);
        }
        if (avatarInfo["headgear"] != "" && avatarInfo["headgear"] != null) {
            defCls = getDefinitionByName("sheet" + avatarInfo["headgear"]) as Class;
            _spritesheetInfo["headgear"] = new defCls(883, 582);
        }
        if (avatarInfo["backHeadgear"] != "" && avatarInfo["backHeadgear"] != null) {
            defCls = getDefinitionByName("sheet" + avatarInfo["backHeadgear"]) as Class;
            _spritesheetInfo["backHeadgear"] = new defCls(883, 582);
        }
        if (avatarInfo["chaussures"] != "" && avatarInfo["chaussures"] != null) {
            defCls = getDefinitionByName("sheet" + avatarInfo["chaussures"]) as Class;
            _spritesheetInfo["chaussures"] = new defCls(883, 582);
        }
        if (avatarInfo["coupe"] != "" && avatarInfo["coupe"] != null) {
            defCls = getDefinitionByName("sheet" + avatarInfo["coupe"]) as Class;
            _spritesheetInfo["coupe"] = new defCls(440, 245);
        }
        if (avatarInfo["frange"] != "" && avatarInfo["frange"] != null) {
            defCls = getDefinitionByName("sheet" + avatarInfo["frange"]) as Class;
            _spritesheetInfo["frange"] = new defCls(440, 245);
        }


        var bit:BitmapData = new BitmapData(36, 48, true, 0);
        bit.copyPixels(currSS, new Rectangle(72, 0, 36, 48), new Point(0, 0));

        var num:int = 0
        var layer:int = 0
        // On habille l'avatar
        var size:int = Math.floor(bit.width / 36);
        var currimg:BitmapData;
        var lala:int;
        var diffx:int;
        var hairPalette:Palette;
        var corpsPalette:Palette;
        var eyePalette:Palette
        var basePoint:Point = new Point(0, 0);
        var decalePoint:Point = new Point(0, 24);
        var baseRect:Rectangle = new Rectangle(0, 0, 48, 48);
        hairPalette = HairPaletteData.getPalette(avatarInfo["couleur"] + "_palette");
        var redArray:Array = hairPalette.red;
        var greenArray:Array = hairPalette.green;
        var blueArray:Array = hairPalette.blue;

        corpsPalette = BodyPaletteData.getPalette(avatarInfo["corps"] + "_palette");
        var redArray2:Array = corpsPalette.red;
        var greenArray2:Array = corpsPalette.green;
        var blueArray2:Array = corpsPalette.blue;

        eyePalette = EyePaletteData.getPalette(avatarInfo["yeux"] + "_palette");
        var redArray3:Array = eyePalette.red;
        var greenArray3:Array = eyePalette.green;
        var blueArray3:Array = eyePalette.blue;

        redArray2 = cumule_palette(redArray2, redArray3);
        blueArray2 = cumule_palette(blueArray2, blueArray3);
        greenArray2 = cumule_palette(greenArray2, greenArray3);

        currimg = new BitmapData(48, 60, true, 0);
        bit.paletteMap(bit, new Rectangle(0, 0, 36, 48), basePoint, redArray2, greenArray2, blueArray2);
        currimg.copyPixels(bit, new Rectangle(0, 0, 36, 48), new Point(6, 12))

        if (_spritesheetInfo["chaussures"] != null) {
            currimg.copyPixels(_spritesheetInfo["chaussures"], new Rectangle(13, 54, 36, 48), new Point(6, 12), null, null, true);
        }
        if (_spritesheetInfo["bas"] != null) {
            currimg.copyPixels(_spritesheetInfo["bas"], new Rectangle(13, 54, 36, 48), new Point(6, 12), null, null, true);
        }
        if (_spritesheetInfo["haut"] != null) {
            currimg.copyPixels(_spritesheetInfo["haut"], new Rectangle(13, 54, 36, 48), new Point(6, 12), null, null, true);
        }

        if (_spritesheetInfo["backHeadgear"] != null) {
            var middleimage:BitmapData = new BitmapData(48, 60, true, 0);

            middleimage.copyPixels(_spritesheetInfo["backHeadgear"], baseRect, new Point(-6, 7), null, null, true);
            middleimage.copyPixels(currimg, new Rectangle(0, 0, 48, 60), basePoint, null, null, true);

            currimg = middleimage;
        }
        if (_spritesheetInfo["coupe"] != null) {
            _spritesheetInfo["coupe"].paletteMap(_spritesheetInfo["coupe"], baseRect, basePoint, redArray, greenArray, blueArray);
            currimg.copyPixels(_spritesheetInfo["coupe"], baseRect, new Point(-6, 7), null, null, true);
        }
        if (_spritesheetInfo["frange"] != null) {
            _spritesheetInfo["frange"].paletteMap(_spritesheetInfo["frange"], baseRect, basePoint, redArray, greenArray, blueArray);
            currimg.copyPixels(_spritesheetInfo["frange"], baseRect, new Point(-6, 7), null, null, true);
        }
        if (_spritesheetInfo["headgear"] != null) {
            currimg.copyPixels(_spritesheetInfo["headgear"], new Rectangle(-1, 0, 36, 48), new Point(-6, 7), null, null, true);
        }

        if (_spritesheetInfo["haut"] != null) {
            _spritesheetInfo["bras"].paletteMap(_spritesheetInfo["bras"], new Rectangle(72, 0, 72, 48), new Point(72, 0), redArray2, greenArray2, blueArray2);

            currimg.copyPixels(_spritesheetInfo["bras"], new Rectangle(72, 0, 72, 48), new Point(6, 12), null, null, true);
            currimg.copyPixels(_spritesheetInfo["haut"], new Rectangle(0, 103, 48, 48), new Point(-7, 12), null, null, true);
        }

        _avatarImage = currimg;

        var tmpBit:BitmapData;
        for each(tmpBit in _spritesheetInfo) {
            tmpBit.dispose();
        }
        _spritesheetInfo = null;
        _hairPalette = null;
        _eyePalette = null;
        _bodyPalette = null;

        HairPaletteData.cleanup();
        EyePaletteData.cleanup();
        BodyPaletteData.cleanup();
    }

    private function cumule_palette(arr:Array, arr2:Array):Array {
        for (var key:String in arr2) {
            arr[key] = arr2[key];
        }
        return arr;
    }

    public function getView():BitmapData {
        return _avatarImage;
    }
}
}